package com.example.Microblogging.controller;

import com.example.Microblogging.dao.PostRepository;
import com.example.Microblogging.entity.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("post")
public class PostController {

    @Autowired
    PostRepository postRepository;

    @GetMapping("")
    public String DisplayPost(Model model){
        List<Post> posts = postRepository.findAll();
        model.addAttribute("post",posts);
        return "";
    }

    @GetMapping("/new")
    public String DisplayPostForm(Model model){
        Post aPost = new Post();
        model.addAttribute("posts",aPost);
        return "";
    }

    @PostMapping("/save")
    public String CreatePost(Model model,Post post){
        postRepository.save(post);
        return "";
    }

}
